<?php
namespace app\api\controller\v1;
use app\api\controller\Common;

class Member extends Common{
	var $modelDb;
	public function _initialize(){
		parent::_initialize();
		$this->modelDb = model("Member");
	}
	public function register(){
		if (isset($this->postData['qqkey']) && !isset($this->postData['phone'])) {
			$info = $this->modelDb->where('qqkey', $this->postData['qqkey'])->find();
			if (!$info) {
				return $this->app_error('需要先绑定账号');
			}
			else{
				model("Jifen")->save_datas(['userid'=>$info['id'], 'taskid'=>5]);
				$info['token'] = model("MemberToken")->create_token($info['id'],$info['username']);
			}
		}
		else{
			$id = $this->modelDb->save_data($this->postData);
	        if($id === false){
	            return $this->app_error($this->modelDb->getError());
	        }
	        // if ($this->postData['recom']) {
	        // 	$recom_userid = db("Member")->where('recom', $this->postData['recom'])->value('id');
	        // 	$data = ['userid'=>$id, 'recom_userid'=>$recom_userid, 'recom'=>$this->postData['recom']];
	        // 	model("MemberRecom")->save_data($data);
	        // }
	        model("Statistics")->add_log(1);
	        model("Jifen")->save_datas(['userid'=>$id, 'taskid'=>4]);
	        $info = $this->modelDb->get_data(['item_id'=>$id]);
	        $info['token'] = model("MemberToken")->create_token($id,$info['username']);
		}
    	return $this->app_success($info);
    }
	public function login(){
		$phone = $this->postData['phone'];
		$password = md5($this->postData['password']);
		$result = $this->validate($this->postData,'Member.login');
		if(true !== $result){
		    return $this->app_error($result);
		}

		$id = $this->modelDb->login($phone,$password);
		if($id === false){
			return $this->app_error("用户名/手机号/邮箱或密码输入错误！");
		}
		// model("Jifen")->save_datas(['userid'=>$id, 'taskid'=>5]);
		$mdata = $this->modelDb->get_data(['item_id'=>$id]);
		$mdata['token'] = model("MemberToken")->create_token($id,$mdata['username']);
		// $mdata['guanzhu'] = model('Favorites')->where('userid', $id)->where('typeid', 1)->count();
		// $mdata['favs'] = model('Favorites')->where('userid', $id)->where('typeid', 2)->count();
		// $mdata['issign'] = model('Jifen')->get_sign($id);
		return $this->app_success($mdata);
	}
	public function edit_pwd(){
		$this->postData['phone'] = $this->uinfo['phone'];
		$result = $this->validate($this->postData, 'Member.edit_pwd');
		if ($result !== true) {
			return $this->app_error($result);
		}
		$id = $this->modelDb->forget_pwd($this->postData);
		if ($id) {
			return $this->app_success([], '修改成功');
		}else{
			return $this->app_error('修改失败');
		}
	}
	public function add_chat_log(){
		$userid = $this->uinfo['id'];
		$chatid = $this->postData['chatid'];
		$msg = $this->postData['msg'];
		$saveData = ['userid' => $userid,'chatid' => $chatid, 'msg' => $msg,'updatetime' => SYS_TIME];
		$iscount = db("MemberChat")->where('userid',$userid)->where("chatid",$chatid)->count();
		if($iscount){
			$saveData = ['msg' => $msg,'updatetime' => SYS_TIME];
			db("MemberChat")->where('userid',$userid)->where("chatid",$chatid)->update($saveData);
		}
		else{
			db("MemberChat")->insert($saveData);
		}
		$this->app_success();
	}
	public function get_chat_list(){
		$userid = $this->uinfo['id'];
		$page = isset($this->postData['page']) && $this->postData['page'] ? $this->postData['page'] :1;
		$map = ['userid' => $userid];
		$list = db("MemberChat")->where('userid',$userid)->page($page, 10)->order("id","desc")->select();
		$data = [];
		$sysdata = [];
		foreach($list as $v){
			$uinfo = model("Member")->get_data(['item_id'=>$v['chatid']]);
			if ($v['chatid'] == 1) {
				$sysdata[] = [
					'id' => $v['id'],
					'username' => $uinfo['nickname'],
					'nickname' => $uinfo['nickname'],
					'avatar' => $uinfo['avatar'],
					'userid' => $v['chatid'],
					'updatetime' => $v['updatetime'],
					'msg' => $v['msg'],
				];
			}else{
				$data[] = [
					'id' => $v['id'],
					'username' => $uinfo['nickname'],
					'nickname' => $uinfo['nickname'],
					'avatar' => $uinfo['avatar'],
					'userid' => $v['chatid'],
					'updatetime' => $v['updatetime'],
					'msg' => $v['msg'],
				];
			}
		}
		$datas = array_merge($sysdata, $data);
		$this->app_success($datas);
	}
	public function chat_del(){
		$ids = db("MemberChat")->where('id', $this->postData['id'])->delete();
		if ($ids) {
			return $this->app_success([], '删除成功');
		}else{
			return $this->app_error('删除失败');
		}
	}
	public function memberinfo(){
		$userid = isset($this->postData['userid']) && $this->postData['userid'] ? $this->postData['userid'] : $this->uinfo['id'];
		$data = $this->modelDb->get_data(['item_id'=>$userid]);
		// $data['guanzhu'] = model('Favorites')->where('userid', $userid)->where('typeid', 1)->count();
		// $data['favs'] = model('Favorites')->where('userid', $userid)->where('typeid', 2)->count();
		// $data['issign'] = model('Jifen')->get_sign($userid);
		return $this->app_success($data);
	}
	public function update(){
		if (!$this->postData['value']) {
			return $this->app_error('传值不能为空');
		}
		if(isset($this->postData['field'])){
			$this->postData[$this->postData['field']] = $this->postData['value'];
		}
		$this->postData['id'] = $this->uinfo['id'];
		$id = $this->modelDb->update_data($this->postData);
		if ($id) {
			$info = $this->modelDb->get_data(['item_id'=>$id]);
			return $this->app_success($info);
		}else{
			return $this->app_error();
		}
	}
	public function bind_phone(){
		$id = $this->uinfo['id'];
		$result = $this->validate($this->postData, 'Member.bind_phone');
		if ($result !== true) {
			return $this->app_error($result);
		}
		$this->postData['id'] = $id;
		$id = $this->modelDb->update_data($this->postData);
		if ($id) {
			return $this->app_success();
		}else{
			return $this->app_error();
		}
	}
	public function bind_alipayno(){
		$id = $this->uinfo['id'];
		$result = $this->validate($this->postData, 'Member.bind_alipayno');
		if ($result !== true) {
			return $this->app_error($result);
		}
		$this->postData['id'] = $id;
		$id = $this->modelDb->update_data($this->postData);
		if ($id) {
			return $this->app_success();
		}else{
			return $this->app_error();
		}
	}
	public function get_token(){
    	$avatar = urlencode($this->uinfo['avatar']);
    	$data = get_token($this->uinfo['id'], $this->uinfo['realname'] ? $this->uinfo['realname'] : $this->uinfo['phone'], $avatar);
    	if ($data['code'] == 200) {
    		return $this->app_success(['rc_token'=>$data['token']]);
    	}else{
    		return $this->app_error('获取token失败');
    	}
    }
    public function send_msg(){
    	// $this->uinfo = $this->modelDb->get_data(['item_id'=>1]);
    	$this->postData['userid'] = $this->uinfo['id'];
    	$this->postData['name'] = $this->uinfo['realname'] ? $this->uinfo['realname'] : $this->uinfo['phone'];
    	$this->postData['avatar'] = urlencode($this->uinfo['avatar']);
    	$this->postData['typeid'] = 1;
    	// $ids = model("ChatRecord")->save_data($this->postData);
    	// if (!$ids) {
    	// 	return $this->app_error(model("ChatRecord")->getError());
    	// }
    	$data = send_msg($this->postData);
    	if ($data['code'] == 200) {
    		return $this->app_success();
    	}else{
    		return $this->app_error();
    	}
    }
    public function del_msg(){
    	$data = del_msg();
    	if ($data['code'] == 200) {
    		return $this->app_success();
    	}else{
    		return $this->app_error();
    	}
    }

    public function zhifu_pwd (){
        $result = $this->validate($this->postData, 'Member.zhifu_pwd');
		if ($result !== true) {
			return $this->app_error($result);
		}
        $this->postData['id'] = $this->uinfo['id'];
        $this->postData['paypassword'] = md5($this->postData['paypassword']);
		$id = $this->modelDb->update_data($this->postData);
		if ($id) {
			return $this->app_success([],'设置成功!');
		}else{
			return $this->app_error('设置失败!');
		}
       	
    }
    public function test_paypwd (){
    	$userid = $this->uinfo['id'];
        $paypassword =  $this->modelDb->where('id',$userid)->value('paypassword');
    	if($paypassword) {
			return $this->app_success(['status'=>1],'已经设置支付密码');
		}else{
			return $this->app_success(['status'=>0],'未设置支付密码');
		}
    }
}